package com.funmi.lizip.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.funmi.lizip.common.base.R;
import com.funmi.lizip.common.base.ResultCode;
import com.funmi.lizip.common.exception.ApplicationException;
import com.funmi.lizip.entity.ProcessIndicator;
import com.funmi.lizip.entity.dto.processIndicator.ProcessIndicatorAddRequest;
import com.funmi.lizip.entity.dto.processIndicator.ProcessIndicatorQueryRequest;
import com.funmi.lizip.entity.dto.processIndicator.ProcessIndicatorUpdateRequest;
import com.funmi.lizip.entity.vo.ProcessIndicatorVo;
import com.funmi.lizip.service.ProcessIndicatorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "生态监测体系运行")
@RestController
@RequestMapping("/indicator")
public class ProcessIndicatorController {


    @Autowired
    private ProcessIndicatorService processIndicatorService;


//    @ApiOperation(value = "获取列表")
//    @GetMapping("/process")
//    public R process() {
//        List<ProcessIndicator> processIndicators = processIndicatorService.getAll();
//
//
//        List<ProcessIndicatorVo> indicatorVos = processIndicators.stream().map(processIndicator -> {
//            ProcessIndicatorVo processIndicatorVo = new ProcessIndicatorVo();
//            BeanUtils.copyProperties(processIndicator, processIndicatorVo);
//            return processIndicatorVo;
//        }).collect(Collectors.toList());
//
//        return R.success().data(indicatorVos);
//    }

//    @ApiOperation(value = "分页展示数据")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "page", value = "当前页码", defaultValue = "1"),
//            @ApiImplicitParam(name = "pageSize", value = "每页的数量", defaultValue = "10")
//    })
//    @GetMapping("")
//    public R processIndicatorPage(int page, int pageSize) {
//        QueryWrapper<ProcessIndicator> qw = new QueryWrapper<>();
//
//        Page<ProcessIndicator> PageDto = new Page<>(page, pageSize);
//        Page<ProcessIndicator> processIndicatorPage = processIndicatorService.page(PageDto, qw);
//        PageDTO<ProcessIndicatorVo> processIndicatorVoPage = new PageDTO<>(page, pageSize, processIndicatorPage.getTotal());
//
//        List<ProcessIndicatorVo> processIndicatorVoList = processIndicatorPage.getRecords().stream().map(processIndicator -> {
//            ProcessIndicatorVo processIndicatorVo = new ProcessIndicatorVo();
//            BeanUtils.copyProperties(processIndicator, processIndicatorVo);
//            return processIndicatorVo;
//        }).collect(Collectors.toList());
//
//
//        processIndicatorVoPage.setRecords(processIndicatorVoList);
//        return R.success().data(processIndicatorVoPage);
//
//    }

    @ApiOperation(value = "根据输入进行检索")
    @GetMapping("")
    public R pageList(ProcessIndicatorQueryRequest request) {
        long current = 1;
        long size = 5;
        ProcessIndicator processIndicatorQuery = new ProcessIndicator();
        if (request != null) {
            BeanUtils.copyProperties(request, processIndicatorQuery);
            current = request.getPage();
            size = request.getPageSize();
        }

        QueryWrapper<ProcessIndicator> qw =new QueryWrapper<>();
        Page<ProcessIndicator> processIndicatorPage = processIndicatorService.page(new Page<>(current, size), qw);
        PageDTO<ProcessIndicatorVo> processIndicatorVoPage = new PageDTO<>(processIndicatorPage.getCurrent(), processIndicatorPage.getSize(), processIndicatorPage.getTotal());

        List<ProcessIndicatorVo> processIndicatorVoList = processIndicatorPage.getRecords().stream().map(processIndicator -> {
            ProcessIndicatorVo processIndicatorVo = new ProcessIndicatorVo();
            BeanUtils.copyProperties(processIndicator, processIndicatorVo);
            return processIndicatorVo;
        }).collect(Collectors.toList());

        processIndicatorVoPage.setRecords(processIndicatorVoList);
        return R.success().data(processIndicatorPage);
    }


    @ApiOperation(value = "新增")
    @PostMapping("")
    public R addProcessIndicator(@RequestBody ProcessIndicatorAddRequest request) {

        if (request == null) {
            throw new ApplicationException(ResultCode.PARAMS_ERROR);
        }

        ProcessIndicator processIndicator = new ProcessIndicator();

        request.setCreateAt(LocalDateTime.now());
        BeanUtils.copyProperties(request, processIndicator);

        boolean save = processIndicatorService.save(processIndicator);

        if (save) {
            return R.success().message("新增成功");
        } else {
            throw new ApplicationException("新增失败");
        }

    }

    @ApiOperation(value = "删除")
    @DeleteMapping("/{id}")
    public R deleteProcessIndicator(@PathVariable int id) {
        if (id <= 0) {
            throw new ApplicationException(ResultCode.ID_NOT_FOUND);
        }

        boolean remove = processIndicatorService.removeById(id);
        if (remove) {
            return R.success().message("删除成功");
        } else {
            return R.fail().message("删除失败");
        }
    }

    @ApiOperation(value = "修改")
    @PutMapping("/{id}")
    public R updateProcessIndicator(@RequestBody ProcessIndicatorUpdateRequest request) {

        if (request == null || request.getId() == null) {
            throw new ApplicationException(ResultCode.PARAMS_ERROR);
        }

        ProcessIndicator processIndicator = new ProcessIndicator();
        BeanUtils.copyProperties(request, processIndicator);

        boolean update = processIndicatorService.updateById(processIndicator);

        if (update) {
            return R.success().message("修改成功");
        } else {
            throw new ApplicationException("修改失败");
        }

    }
}
